﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.IO;
using BLL;

namespace UI
{
    public partial class FGetOnline : Form
    {
        private Http http = new Http();
        private string strSessionID;
        private string strECardCookie;
        private static readonly int iECardID = 1;
        private static readonly int iBankID = 2;

        public FGetOnline()
        {
            InitializeComponent();
        }

        private void bt_Get_Click(object sender, EventArgs e)
        {
            txb_log.AppendText(DateTime.Now.ToString() + "：努力获取数据中……\r\n");
            string strName = txb_Name.Text;
            string strPassword = txb_Password.Text;
            if (strName.Length == 0 | strPassword.Length == 0)
            {
                txb_log.AppendText(DateTime.Now.ToString() + "：请输入用户名或密码！\r\n");
                return;
            }
            string strQuery = "";
            switch (cb_Source.SelectedIndex)
            {
                case 0:
                    strSessionID = http.GetCookie("http://portal.tsinghua.edu.cn");
                    if (strSessionID != null && http.CheckInfo(strName, strPassword, strSessionID))
                        txb_log.AppendText(DateTime.Now.ToString() + "：用户名或密码错误！\r\n");
                    try
                    {
                        Record rBank = FMain.rcdMan.GetLastRecord(iBankID);
                        if (rBank != null)
                        {
                            for (int i = 0; rBank.Time.AddMonths(i) < DateTime.Now.AddMonths(-1); i++)
                            {
                                strQuery = strQuery + "year=" + rBank.Time.AddMonths(i + 1).ToString("yyyy#MM") + "&";
                            }
                            //删除最后一个&
                            strQuery.Remove(strQuery.Length - 1, 1);
                        }
                        else
                        {
                            strQuery = "year=2011%2312&year=2011%2311&year=2011%2310&year=2011%239&year=2011%238&year=2011%237&year=2011%236&year=2011%235";
                        }
                        string strData = http.GetBankData(strSessionID, strQuery);
                        RecordCollection rc = http.AnanlyzeBankData(strData);
                        FMain.rcdMan.AddRecord(rc, false);
                        double dSum = CalcAmount(rc);
                        FMain.accMan.GetAccount(iBankID).Balance -= dSum;
                        FMain.rcdMan.SaveDataToFile();
                        FMain.accMan.SaveDataToFile();
                        txb_log.AppendText(DateTime.Now.ToString() + "：银行代发数据获取成功！\r\n");
                    }
                    catch (System.Exception ex)
                    {
                        txb_log.AppendText(DateTime.Now.ToString() + "：" + ex.Message + "\r\n");

                    }


                    break;
                case 1:
                    string strCookie = strECardCookie;
                    string strVertification = txb_Vertification.Text;
                    Record rECard = FMain.rcdMan.GetLastRecord(iECardID);
                    if (rECard != null)
                    {
                        strQuery = "begindate=" + rECard.Time.AddDays(1).ToString("yyyy-MM-dd") + "&enddate=&transtype=&dept=";
                    }
                    else
                    {
                        strQuery = "begindate=&enddate=&transtype=&dept=";
                    }
                    if (http.CheckECard(strName, strPassword, strVertification, strCookie))
                    {
                        bool bContinue = true;
                        int i = 1;
                        string s = strQuery;
                        while (bContinue)
                        {
                            RecordCollection rc = http.AnalyzeECardData(http.GetECardData(strCookie, s), out bContinue);
                            FMain.rcdMan.AddRecord(rc, !bContinue);
                            double dSum = CalcAmount(rc);
                            FMain.accMan.GetAccount(iECardID).Balance -= dSum;
                            s = strQuery + "&dir=next&currentPage=" + i.ToString();
                            i++;
                        }
                        txb_log.AppendText(DateTime.Now.ToString() + "：一卡通数据获取成功！\r\n");
                        FMain.rcdMan.SaveDataToFile();
                        FMain.accMan.SaveDataToFile();
                    }
                    else
                        txb_log.AppendText(DateTime.Now.ToString() + "：用户名或密码错误！\r\n");
                    break;
                default:
                    txb_log.AppendText(DateTime.Now.ToString() + "：未选择数据来源！\r\n");
                    break;
            }
        }

        private void pb_Click(object sender, EventArgs e)
        {
            txb_log.AppendText(DateTime.Now.ToString() + "：正在获取验证码……\r\n");
            try
            {

                strECardCookie = http.GetRecognition(pb);
                txb_log.AppendText(DateTime.Now.ToString() + "：获取验证码成功\r\n");
            }
            catch (System.Exception ex)
            {
                txb_log.AppendText(DateTime.Now.ToString() + "：" + ex.Message + "\r\n");
            }
        }

        private void cb_Source_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_Source.SelectedIndex == 1)
                pl.Visible = true;
            else pl.Visible = false;

        }

        private void txb_log_TextChanged(object sender, EventArgs e)
        {
            if (txb_log.TextLength > 351)
            {
                txb_log.Text = "";
            }
        }

        private double CalcAmount(RecordCollection rc)
        {
            double dSum = 0;
            for (int i = 0; i < rc.Count; i++)
            {
                if (FMain.typMan.GetType(rc[i].TypeID).Isincome)
                    dSum -= rc[i].Amount;
                else
                    dSum += rc[i].Amount;
            }
            return dSum;
        }
    }
}
